import 'package:built_collection/built_collection.dart';
import 'package:dartz/dartz.dart';
import 'package:equatable/equatable.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/app_navigator_transition_info.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/page_models/app_navigator_page_model.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/page_models/single_word_base_page_model.dart';
import 'package:learning_content_admin_pannel.core/core/domain/helpers/handle_root_navigator_page_closing_request/use_case.dart';
import 'package:learning_content_admin_pannel.core/core/domain/services/lexical_item_image_caching_service/service.dart';

part 'failures.dart';

part 'result.dart';

abstract class HandleLearningContentOuterUpdate {
  Either<LearningContentOuterUpdateHandlingFailure, LearningContentOuterUpdateHandlingResult> call({
    required SingleWordBasePageModel pageModel,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
  });
}

class HandleLearningContentOuterUpdateImpl implements HandleLearningContentOuterUpdate {
  const HandleLearningContentOuterUpdateImpl({
    required HandleRootNavigatorPageClosingRequest handleRootNavigatorPageClosingRequest,
    required LexicalItemImageCachingService lexicalItemImageCachingService,
  })  : _handleRootNavigatorPageClosingRequest = handleRootNavigatorPageClosingRequest,
        _lexicalItemImageCachingService = lexicalItemImageCachingService;

  final HandleRootNavigatorPageClosingRequest _handleRootNavigatorPageClosingRequest;
  final LexicalItemImageCachingService _lexicalItemImageCachingService;

  @override
  Either<LearningContentOuterUpdateHandlingFailure, LearningContentOuterUpdateHandlingResult> call({
    required SingleWordBasePageModel pageModel,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
  }) {
    final word = lexicalItemMap[pageModel.wordId] as Word?;

    if (word == null) {
      const transitionInfo = RemovalNavigatorTransitionInfo(
        hasAnimation: false,
      );

      final rootNavigatorPagePushHandlingResult = _handleRootNavigatorPageClosingRequest(
        pageModel: pageModel,
        rootNavigatorPageModels: rootNavigatorPageModels,
        rootNavigatorPageModelToTransitionInfo: rootNavigatorPageModelToTransitionInfo,
        transitionInfo: transitionInfo,
      );

      final failure = NonexistentWordLearningContentOuterUpdateHandlingFailure(
        rootNavigatorPageModels: rootNavigatorPagePushHandlingResult.rootNavigatorPageModels,
        rootNavigatorPageModelToTransitionInfo:
            rootNavigatorPagePushHandlingResult.rootNavigatorPageModelToTransitionInfo,
      );

      return left(failure);
    }

    String? cachedWordImagePath;

    if (word.imageId != null) {
      cachedWordImagePath = _lexicalItemImageCachingService.cachedImagePathMap[word.imageId];
    }

    final result = LearningContentOuterUpdateHandlingResult(
      cachedWordImagePath: cachedWordImagePath,
    );

    return right(result);
  }
}
